package com.example.springbootlogin.service.serviceImpl;

import com.example.springbootlogin.domain.User;
import com.example.springbootlogin.repository.UserDao;
import com.example.springbootlogin.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Override
    public User loginService(String uname, String password) {
        User user = userDao.findByUnameAndPassword(uname, password);
        return user;
    }

    @Override
    public User registService(User user) {
        if(userDao.findByUname(user.getUname())!=null){
            return null;
        }else{
            User newUser = userDao.save(user);
            if(newUser != null){
                newUser.setPassword("");
            }
            return newUser;
        }
    }
    
    @Override
    public List<User> selectAllUsers() {
        List<User> users = userDao.findAll();
        // 不返回密码
        return users.stream().peek(user -> user.setPassword("")).collect(Collectors.toList());
    }
}
